package com.hnhd.eorder.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 执行数据库初始化操作
 * 
 * @author peng
 *
 */
public class DatabaseHelper extends SQLiteOpenHelper{
	
	public  static final int DB_VERSION = 1;
	
	private static final String DATABASE_NAME = "eorder.db";
	
	
	
	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	
	public DatabaseHelper(Context context) {
		this(context,DATABASE_NAME,null,DB_VERSION);
	}



	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String TABLE_ORDER_CREATE = "create table "+ SQLUtil.ORDER 
			+ "("+SQLUtil.ORDER_ID+ " integer primary key autoincrement, "
			+SQLUtil.ORDER_CASHIER+ " integer not null,"
			+SQLUtil.ORDER_DISCOUNT+ " float not null,"
			+SQLUtil.ORDER_ENDTIME+ " datetime not null,"
			+SQLUtil.ORDER_ORDERTIME+ " datetime not null,"
			+SQLUtil.ORDER_NUMBER+ " integer not null,"
			+SQLUtil.ORDER_PRICE+ " float not null,"
			+SQLUtil.ORDER_REMARKS+ " text not null,"
			+SQLUtil.ORDER_SERVER+ " text not null,"
			+SQLUtil.ORDER_STATUS+ " text not null,"
			+SQLUtil.ORDER_TABLEID+ " integer not null);";
	

	private static final String TABLE_DISH_CREATE = "create table "	+ SQLUtil.DISH 
			+ "(" + SQLUtil.DISH_ID+ " integer primary key autoincrement, "
			+SQLUtil.DISH_BRIEF+ " text not null,"
			+SQLUtil.DISH_ISGUQING+ " integer not null,"
			+SQLUtil.DISH_LARGEPIC+ " text not null,"
			+SQLUtil.DISH_MEDIUMPIC+ " text not null,"
			+SQLUtil.DISH_MODEL+ " text not null,"
			+SQLUtil.DISH_NAME+ " text not null,"
			+SQLUtil.DISH_NORM+ " text not null,"
			+SQLUtil.DISH_PRICE+ " float not null,"
			+SQLUtil.DISH_REMAIN+ " integer not null,"
			+SQLUtil.DISH_SMALLPIC+ " text not null);";
	

	private static final String TABLE_TYPE_CREATE = "create table "
			+ SQLUtil.TYPE + "(" + SQLUtil.TYPE_ID
			+ " integer primary key autoincrement, "
			+SQLUtil.TYPE_NAME+ " text not null);";
	

	private static final String TABLE_TABLE_CREATE = "create table "
			+ SQLUtil.TABLE + "(" + SQLUtil.TABLE_ID
			+ " integer primary key autoincrement, "
			+SQLUtil.TABLE_BRIEF+ " text not null,"
			+SQLUtil.TABLE_NAME+ " text not null,"
			+SQLUtil.TABLE_NUMBER+ " integer not null,"
			+SQLUtil.TABLE_ROOMID+ " integer not null,"
			+SQLUtil.TABLE_STATUS+ " text not null);";
	

	private static final String TABLE_ROOM_CREATE = "create table "
			+ SQLUtil.ROOM + "(" + SQLUtil.ROOM_ID
			+ " integer primary key autoincrement, "
			+SQLUtil.ROOM_BRIEF+ " text not null,"
			+SQLUtil.ROOM_NAME+ " text not null,"
			+SQLUtil.ROOM_TABLENUM+ " integer not null,"
			+SQLUtil.ROOM_HASAIRCONDITION+ " text not null,"
			+SQLUtil.ROOM_HASBATHROOM+ " text not null,"
			+SQLUtil.ROOM_HASMAHJONG+ " text not null);";
	

	private static final String TABLE_ORDERITEM_CREATE = "create table "
			+ SQLUtil.ORDERITEM + "(" + SQLUtil.ORDERITEM_ID
			+ " integer primary key autoincrement, "
			+SQLUtil.ORDERITEM_DISHID+ " integer not null,"
			+SQLUtil.ORDERITEM_DISCOUNT+ " float not null,"
			+SQLUtil.ORDERITEM_ISPRINT+ " integer not null,"
			+SQLUtil.ORDERITEM_NUMBER+ " integer not null,"
			+SQLUtil.ORDERITEM_ORDERID+ " integer not null,"
			+SQLUtil.ORDERITEM_PRICE+ " float not null,"
			+SQLUtil.ORDERITEM_REMARKS+ " text not null,"
			+SQLUtil.ORDERITEM_STATUS+ " text not null);";
	

	private static final String TABLE_DISHTYPE_CREATE = "create table "
			+ SQLUtil.DISHTYPE + "(" + SQLUtil.DISHTYPE_DISHID
			+ " integer not null, "
			+SQLUtil.DISHTYPE_TYPEID+ " integer not null);";


	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL("DROP TABLE IF EXISTS " + SQLUtil.DISH);
		database.execSQL(TABLE_DISH_CREATE);
		database.execSQL("DROP TABLE IF EXISTS " + SQLUtil.TYPE);
		database.execSQL(TABLE_TYPE_CREATE);
		database.execSQL("DROP TABLE IF EXISTS " + SQLUtil.DISHTYPE);
		database.execSQL(TABLE_DISHTYPE_CREATE);
		database.execSQL("DROP TABLE IF EXISTS " + SQLUtil.ORDER);
		database.execSQL(TABLE_ORDER_CREATE);
		database.execSQL("DROP TABLE IF EXISTS " + SQLUtil.ORDERITEM);
		database.execSQL(TABLE_ORDERITEM_CREATE);
		database.execSQL("DROP TABLE IF EXISTS " + SQLUtil.ROOM);
		database.execSQL(TABLE_ROOM_CREATE);
		database.execSQL("DROP TABLE IF EXISTS " + SQLUtil.TABLE);
		database.execSQL(TABLE_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DatabaseHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		onCreate(db);
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		super.onOpen(db);
	}

}
